package com.welab.task3.service.impl;

import com.welab.task3.dao.UserMapper;
import com.welab.task3.dto.response.Result;
import com.welab.task3.entity.User;
import com.welab.task3.service.UserService;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * @author dammein.yuan
 * @version v1.0
 * @Title: com.welab.task3.service.impl
 * @Description: 用户 service 层接口实现类
 * @Copyright: © 2020 ***
 * @Company: ***有限公司
 * @date 2020-3-9
 */
@Service
public class UserServiceImpl implements UserService {

    @Autowired
    private UserMapper userMapper;

    @Override
    public Result deleteByUserId(Integer id) {
        int num = userMapper.deleteByUserId(id);
        if(num == 1) {
            return Result.success("删除成功");
        }
        else{
            return Result.fail("删除失败");
        }
    }

    @Override
    public Result register(User user) {
        int num = userMapper.insert(user);
        if(num == 1) {
            return Result.success("注册成功");
        }
        else{
            return Result.fail("注册失败");
        }
    }

    @Override
    public List<User> view() {
        return userMapper.selectAll();
    }

    @Override
    public Result modified(User user) {
        int num = userMapper.update(user);
        if(num == 1){
            return Result.success("修改成功");
        }
        else{
            return Result.fail("修改失败");
        }
    }

    @Override
    public Result login(String name, String password, Integer type) {
        User user = userMapper.selectByNameAndPwdAndType(name, password, type);
        if(user != null){
            return Result.success("登录成功");
        }
        else{
            return Result.fail("登录失败");
        }
    }

    @Override
    public Result adduser(User user) {
        int num = userMapper.insert(user);
        if(num == 1) {
            return Result.success("添加成功");
        }
        else{
            return Result.fail("添加失败");
        }
    }

}
